package com.derbysoft.ota.domain.city;

import com.google.common.base.Preconditions;
import org.apache.commons.lang.StringUtils;

import com.derbysoft.remote.dhis.DHisIATAAirport2CityWebService;
import com.derbysoft.remote.dhis.DHisIATAAirport2CityWebServiceFactory;
import com.derbysoft.remote.dhis.dto.IATAAirport2CityRQ;
import com.derbysoft.remote.dhis.dto.IATAAirport2CityRS;
import com.derbysoft.remote.dswitch.core.GenericRQ;
import com.derbysoft.remote.dswitch.core.GenericRS;
import com.google.inject.Inject;

public class CityCodeServiceImpl implements CityCodeService {

	private DHisIATAAirport2CityWebServiceFactory dHisIATAAirport2CityWebServiceFactory;
	
	@Inject
	public CityCodeServiceImpl(DHisIATAAirport2CityWebServiceFactory dHisIATAAirport2CityWebServiceFactory) {
		this.dHisIATAAirport2CityWebServiceFactory = dHisIATAAirport2CityWebServiceFactory;
	}

	@Override
	public String iata2switch(String iataCityCode) throws IllegalArgumentException {
		Preconditions.checkArgument(StringUtils.isNotBlank(iataCityCode), "The IATA code can not be empty.");
		GenericRS<IATAAirport2CityRS> airport2CityRSGenericRS = dhis().convert(new GenericRQ<IATAAirport2CityRQ>(new IATAAirport2CityRQ(iataCityCode)));
		String dswitchCityCode = airport2CityRSGenericRS.getPayload().getCityCode();
		if (StringUtils.isBlank(dswitchCityCode)) {
			throw new IllegalArgumentException("Sorry, the IATA code [" + iataCityCode + "] not found in our system.");
		}
		return dswitchCityCode;
	}

	private DHisIATAAirport2CityWebService dhis() {
		return dHisIATAAirport2CityWebServiceFactory.getDHisIATAAirport2CityWebService();
	}


}
